package com.invite.me.ui.xieyi;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.invite.me.R;
import com.invite.me.base.BaseActivity;

import java.util.HashMap;
import java.util.Map;

public class ProtocolH5Activity extends BaseActivity implements View.OnClickListener {

    private WebView learch_webview;
    private ImageView account_balance_fanhui;

    @Override
    protected void initView() {
        Intent intent = getIntent();
        String type = intent.getStringExtra("type");
        String uil = intent.getStringExtra("uil");

        account_balance_fanhui = findViewById(R.id.account_balance_fanhui);
        TextView titlename = findViewById(R.id.titlename);
        if(type.equals("one")){
            titlename.setText("软件使用协议");

        }else if(type.equals("two")){
            titlename.setText("个人信息保护及隐私政策");
        }else if(type.equals("three")){
            titlename.setText("用户服务协议");
        }


        learch_webview = findViewById(R.id.learch_webview);
        learch_webview.clearCache(true);

        account_balance_fanhui.setOnClickListener(this);


        WebSettings webSettings = learch_webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setDomStorageEnabled(true);
        learch_webview.loadUrl(uil);
        learch_webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                if (url == null) {
                    return false;
                } else {
                    try {
                        if (url.startsWith("weixin://") || url.startsWith("alipays://")) {
                            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                            startActivity(intent);

                        } else if (url.startsWith("mailto:")) {
                            //Handle mail Urls
                            startActivity(new Intent(Intent.ACTION_SENDTO, Uri.parse(url)));

                        } else if (url.startsWith("tel:")) {
                            //Handle telephony Urls
                            startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));

                        } else {
                            // view.loadUrl(url);
                            //处理http和https开头的url
//                            Map<String, String> extraHeaders = new HashMap<>();
//                            extraHeaders.put("Referer", Constants.API_FENXIANG_URL);
//                            view.loadUrl(url, extraHeaders);
                            return true;
                        }
                    } catch (Exception e) { //防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)
                        return false;
                    }
                }
                return true;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                //  showProgress("页面加载中");//开始加载动画



            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //removeProgress();//当加载结束时移除动画

            }


        });




    }

    @Override
    protected int getActivityLayoutId() {
        return R.layout.activity_protocolh5;
    }

    @Override
    protected void loadData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.account_balance_fanhui:
                finish();
                break;

        }

    }
}
